package com.yinhe.music.yhmusic.search;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hwangjr.rxbus.RxBus;
import com.yinhe.music.common.utils.ScreenUtils;
import com.yinhe.music.event.RxBusEventType;
import com.yinhe.music.event.RxbusNullObject;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.RadioListAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceViewPagerFragment;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.constants.KeyConstants;
import com.yinhe.music.yhmusic.model.RadioListInfo;
import com.yinhe.music.yhmusic.radio.RadioActivity;
import com.yinhe.music.yhmusic.radio.contract.IRadioListContract;
import com.yinhe.music.yhmusic.radio.presenter.RadioListPresenter;
import com.yinhe.music.yhmusic.search.adapter.RadioAdapter;
import com.yinhe.music.yhmusic.utils.UmengEventUtils;
import com.yinhe.music.yhmusic.widget.CustomLoadMoreView;
import com.yinhe.music.yhmusic.widget.DividerItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentLinearLayoutManager;

import java.util.List;

import butterknife.BindView;

/**
 * @author 何晓华
 * @date 2018/3/31
 */

public class RadioListFragment extends BaseServiceViewPagerFragment implements
        BaseQuickAdapter.OnItemClickListener, ISearchFragment, BaseQuickAdapter.RequestLoadMoreListener,
        IRadioListContract.IRadioListView {
    @BindView(R.id.recommend_music_recyclerview)
    RecyclerView mRecyclerView;

    private String key;
    private BaseQuickAdapter mAdapter;
    private int pageNum;
    private int mPageSize = 10;
    private int mPage = 1;
    private int mPageType;
    private RadioListPresenter mPresenter;
    private String userId;


    public static RadioListFragment newInstance(String key, int pageType, String userId) {
        RadioListFragment fragment = new RadioListFragment();

        Bundle bundle = new Bundle();
        bundle.putString("key", key);
        bundle.putInt(KeyConstants.KEY_PAGE_TYPE, pageType);
        bundle.putString(KeyConstants.KEY_USER_ID, userId);

        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new RadioListPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    @Override
    public void initView() {
        if (getArguments() != null && TextUtils.isEmpty(key)) {
            key = getArguments().getString("key");
            mPageType = getArguments().getInt(KeyConstants.KEY_PAGE_TYPE, IConstants.SEARCH_RADIO_LIST);
            userId = getArguments().getString(KeyConstants.KEY_USER_ID);
        }
        setList();
        initAdapter();
    }

    @Override
    public int getLayoutId() {
        return R.layout.music_hall_recycler_layout;
    }

    @Override
    protected void onFragmentFirstVisible() {
        if (mService != null) {
            reqRadioList();
        }
    }

    private void setList() {
        mRecyclerView.setLayoutManager(new WrapContentLinearLayoutManager(getActivity()));
        mRecyclerView.setHasFixedSize(true);
        if (mPageType == IConstants.SEARCH_RADIO_LIST) {
            DividerItemDecoration itemDecoration = new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL_LIST, ScreenUtils.dp2px(20), false);
            mRecyclerView.addItemDecoration(itemDecoration);
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @Override
                public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                    super.onScrollStateChanged(recyclerView, newState);
                    if (newState == RecyclerView.SCROLL_STATE_DRAGGING) {
                        RxBus.get().post(RxBusEventType.Search.HIDE_SOFT_KEYBOARD, RxbusNullObject.INSTANCE);
                    }
                }
            });
        }
    }

    private void initAdapter() {
        if (mPageType == IConstants.SEARCH_RADIO_LIST) {
            mAdapter = new RadioAdapter(null, key);
            mRecyclerView.setAdapter(mAdapter);
        } else {
            mAdapter = new RadioListAdapter();
            mRecyclerView.setAdapter(mAdapter);
        }

        mAdapter.setLoadMoreView(new CustomLoadMoreView());
        mAdapter.setOnLoadMoreListener(this, mRecyclerView);
        mAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        mAdapter.setPreLoadNumber(5);
        mAdapter.setOnItemClickListener(this);
    }

    private void reqRadioList() {
        if (mPageType == IConstants.SEARCH_RADIO_LIST) {
            mPresenter.getSearchList(mPage, mPageSize, key);
        } else {
            mPresenter.getUserRadioList(userId, mPage, mPageSize);
        }

    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        UmengEventUtils.clickShare(getActivity(), UmengEventUtils.RAIDO);
        RadioListInfo info = (RadioListInfo) adapter.getData().get(position);
        Intent intent = new Intent(getActivity(), RadioActivity.class);
        intent.putExtra("radioid", info.getRadioId());
        startActivity(intent);
    }

    @Override
    public void updateDataForKey(String key) {
        if (key.equals(this.key)) {
            return;
        }
        this.key = key;
        if (mAdapter != null) {
            RadioAdapter radioAdapter = (RadioAdapter) mAdapter;
            radioAdapter.setSearchKey(key);
            mPage = 1;
            reqRadioList();
        }
    }

    @Override
    public void onLoadMoreRequested() {
        if (mPage <= pageNum) {
            reqRadioList();
        } else {
            mAdapter.loadMoreEnd();
        }
    }

    @Override
    public void setRadioListUI(RadioListInfo info) {
        mRecyclerView.stopScroll();
        List<RadioListInfo> list = info.getRadioList();
        if (list == null) {
            return;
        }

        pageNum = info.getPageNum();

        if (mPage == 1) {
            mAdapter.setEmptyView(getEmptyView(mRecyclerView));
        }

        int size = list.size();

        if (mPage == 1) {
            mAdapter.setNewData(list);
        } else {
            if (size > 0) {
                mAdapter.addData(list);
            }
        }

        if (mPage >= pageNum) {
            mAdapter.loadMoreEnd();
        } else {
            mPage++;
            mAdapter.loadMoreComplete();
        }
    }

    @Override
    public void setLoadMoreFailUI() {
        mAdapter.loadMoreFail();
    }
}
